package com.hs.sentinelr.config;

import com.hs.sentinelr.aop.RateLimitInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.scripting.support.ResourceScriptSource;

import java.util.List;

@ConditionalOnWebApplication
public class RateLimitConfig {

    @Bean("rateLimitRedisScript")
    public RedisScript<List<Long>> redisScript(){
        DefaultRedisScript script = new DefaultRedisScript<>();
        script.setScriptSource(new ResourceScriptSource(new ClassPathResource("META-INF/scripts/redis_rate_limiter.lua")));
        script.setResultType(List.class);
        return script;
    }

    @Bean
    public RateLimitInterceptor rateLimitInterceptor(){
        return new RateLimitInterceptor();
    }
}
